package phapp.action.barapp;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import net.sf.json.*;
import javacommon.util.*;
import javacommon.jquery.*;
import static javacommon.util.Struts2JsonHelper.*;
import com.opensymphony.xwork2.*;
import cn.org.rapid_framework.page.*;

import phapp.model.barapp.VipMain;

import javacommon.base.*;

import phapp.model.barapp.*;
import phapp.dao.barapp.*;
import phapp.service.barapp.impl.*;
import phapp.service.barapp.*;

/**
 * <p> 标题: 会员主信息表
 * <p>Title: tc_vip_main_info, 
 * <p>Description: 接收前面界面请求,通过json数据格式交互 Action
 * <p>Description: 页面通过Ajax调用Action层,返回JSONO数据
 * <p>Copyright: Copyright (c) 2014-10
 * <p>Company:
 * @see phapp.model.barapp.VipMain
 * @see phapp.service.barapp.VipMainManager 
 * @see phapp.service.barapp.impl.VipMainManagerImpl
 * @see phapp.dao.barapp.VipMainDao
 * @author you name
 * @version 1.0
*/


public class VipMainAction extends BaseStruts2Action {

   private VipMainManager vipmainManager;

   /** 通过spring自动注入 */
   public void setVipMainManager(VipMainManager manager) {
      this.vipmainManager = manager;
   }

   /**
    * <p> 标题:jqGrid使用列表,分页查询
    * @throws IOException
    * @author
    */
   public void list() throws IOException{
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = vipmainManager.findByPageRequest(pr);

      List<VipMain> VipMainlist = (List) page.getResult();
      ListRange<VipMain> resultList = new ListRange<VipMain>();
      resultList.setDataRows(VipMainlist);
        resultList.setRecords(page.getTotalCount());
        resultList.setRows(page.getPageSize());
      if(page.getTotalCount()%page.getPageSize()>0){
         resultList.setTotal(page.getTotalCount() / page.getPageSize()+1);
      }else{
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   
   /**
    * <p>标题:操作数据 修改 状态
    * @author
    * @throws IOException
    */
   public void operating() throws IOException{
      Map<String, Object> result = new HashMap<String, Object>();
      try{
         HttpServletRequest request = getRequest();
         String vipmainJson = request.getParameter("vipmainJson");
         if (null != vipmainJson&&!"".equals(vipmainJson)) {
            JSONObject jsonobject =JSONObject.fromObject( vipmainJson ); 
            String state = jsonobject.get("state").toString();
            String ids = jsonobject.get("ids").toString();  
            if(null!=state&&null!=ids){
            	Map<String, Object> paramMap = new HashMap<String, Object>();
            	paramMap.put("state", state);
            	paramMap.put("id", ids);
            	vipmainManager.updataByMap(paramMap);
            	result.put("success", true);
                result.put("msg", "操作成功!");
            }else{
            	result.put("success", false);
                result.put("msg", "操作失败 数据错误!");
            } 
         }else{
            result.put("success", false);
            result.put("msg", "操作失败 数据错误!");
         }
      }catch (Exception e){
         result.put("success", false);
         result.put("msg", "操作失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
	/**
	 *  <p>标题：下拉框所用 列表 
	 * @throws IOException
	 */
	public void getComboBox() throws IOException { 
		Map<String, Object> result = new HashMap<String, Object>();
		List list = new ArrayList();
		try {
			String str = "";
			VipMain vipmainobj = new VipMain();
			list = vipmainManager.findAll();
	        result.put("success", false);
	        result.put("list", list);
		} catch (Exception e) { 
			e.printStackTrace();
		}
		outJson(list);
	}
	 
    /**
     * <p>标题: 查询列表数据 ByMap
     * <p>描述:  
     * @throws Exception 
     */
    public void  findListByMap() throws Exception{ 
    	Map map = getParamMap();
		List<VipMain> dataList = vipmainManager.findList(map);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put(SUCCESS, true);
		result.put("list", dataList);
		outJson( result);
    }
    /**
     * <p>标题: 查询列表数据 ByBean
     * <p>描述:  
     * @throws Exception 
     */
    public void  findListByBean() throws Exception{
    	Map<String,Object> result = new HashMap<String,Object>();
        List<VipMain> list = new ArrayList();
        try{
           HttpServletRequest request = getRequest();
           String vipmainJson = request.getParameter("vipmainJson");
           if (null != vipmainJson&&!"".equals(vipmainJson)) {
              JSONObject jsonobject =JSONObject.fromObject( vipmainJson );
              VipMain vipmainobj = (VipMain)JSONObject.toBean(jsonobject, VipMain.class); 
              list =  vipmainManager.findListByBean(vipmainobj); 
           }else{
        	  list = vipmainManager.findAll(); 
           }
           result.put(SUCCESS, true);
        }catch (Exception e){ 
        	result.put(SUCCESS, false);
            e.printStackTrace();
        } 
		result.put("list", list);
        outJson(result);
    }	
}
